<?php
/**
 * 导航
 * User: yuanxuncheng
 * Date: 2016-12-21
 * Time: 22:39
 */

namespace app\api\model\dao;

use think\Model;
use think\Db;

class BannerImage extends Model
{
    //set talbename
    protected $table = 'banner_image';
    //set primary key
    protected $pk = 'banner_image_id';

    /**
     * @desc 添加BannerImage
     * @param $param
     * @return false|int
     */
    public function addBannerImage($param)
    {
        return $this->allowField(true)->save($param);
    }

    public function updateBannerImage($banner_image_id, $param)
    {
        $conds = array(
            'banner_image_id' => $banner_image_id,
        );
        $field = array();
        if (isset($param['title'])) {
            $field['title'] = $param['title'];
        }

        if (isset($param['link'])) {
            $field['link'] = $param['link'];
        }

        if (isset($param['image'])) {
            $field['image'] = $param['image'];
        }

        if (isset($param['sort_order'])) {
            $field['sort_order'] = $param['sort_order'];
        }

        return $this->isUpdate(true)->allowField(true)->save($field, $conds);
    }

    public function deleteBannerImage($banner_image_id)
    {
        $conds = array(
            'banner_image_id' => $banner_image_id,
        );
        return Db::table($this->table)->where($conds)->delete();
    }

    /**
     * @desc 获取BannerImage
     * @param $param
     * @return false|int
     */
    public function getBannerImage($banner_id)
    {
        $conds = array(
            'banner_id' => $banner_id,
        );
        $field = '*';
        return Db::table($this->table)->where($conds)->field($field)->select();
    }

    public function getBannerImageList()
    {
        return $this->paginate(10);
    }

    public function getBannerImageBatch($banner_ids)
    {
        $conds = array(
            'banner_id' => ['in', $banner_ids],
        );
        return Db::table($this->table)->where($conds)->select();
    }
}
